﻿using System;
using System.Collections.Generic;
using Castle.Facilities.NHibernateIntegration;
using NHibernate;
using Castle.MicroKernel;
using System.Diagnostics.CodeAnalysis;

namespace Inovout.Repositories
{
    public class TypeSessionManager : DefaultSessionManager, ITypeSessionManager
    {

        private RepositoryConfigurationKeyDictionary repositoryConfigurationKeyDictionary;
        public TypeSessionManager(RepositoryConfigurationKeyDictionary repositoryConfigurationKeyDictionary, ISessionStore sessionStore, IKernel kernel, ISessionFactoryResolver factoryResolver)
            : base(sessionStore, kernel, factoryResolver)
        {
            this.repositoryConfigurationKeyDictionary = repositoryConfigurationKeyDictionary;
        }
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
        public ISession OpenSession<TModel>() where TModel : class
        {
            return OpenSession(typeof(TModel));
        }
    
        public ISession OpenSession(Type modelType)
        {
            string alias = GetAlias(modelType);
            if (string.IsNullOrEmpty(alias))
            {
                return OpenSession();
            }
            else
            {
                return OpenSession(alias);
            }
        }
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
        public IStatelessSession OpenStatelessSession<TModel>() where TModel : class
        {
            return OpenStatelessSession(typeof(TModel));
        }
        public IStatelessSession OpenStatelessSession(Type modelType)
        {
            string alias = GetAlias(modelType);
            if (string.IsNullOrEmpty(alias))
            {
                return null;
            }
            else
            {
                return OpenStatelessSession(alias);
            }
        }
        private static string GetAlias(Type modelType)
        {
            string scope = modelType.FullName + "," + modelType.Assembly.GetName(false).Name;
            scope = null;
            return scope;
        }
    }
}
